app.controller('chapterController',function($scope,$location,$controller, chapterService, chapterLabelService ,chapterCategoryService){
    $controller('baseController',{$scope:$scope,baseService:chapterService});

    $scope.editIsuse = function (id, val, msg) {
        var bool = true;
        if (msg) {
            bool = window.confirm(msg);
        }
        if (bool) {
            var queryJson = {
                entity: {
                    id: id,
                    isuse: val
                }
            };
            chapterService.update(queryJson).success(function (res) {
                $scope.resHandler(res, function (res) {
                    alert("修改成功");
                    $scope.findPage();
                })
            })
        }

    };

    $scope.batchEditIsuse = function (val, msg) {
        var bool = true;
        if (msg) {
            bool = window.confirm(msg);
        }
        if (bool) {
            var queryJson = {
                entity: {
                    isuse: val
                },
                ids : $scope.ids
            };
            chapterService.updateManyByIds(queryJson).success(function (res) {
                $scope.resHandler(res, function (res) {
                    alert("修改成功");
                    $('.js_checkbox').prop("checked", false);
                    $scope.findPage();
                })
            })
        }

    };

    $scope.edit = function(direct){
        var entity = $scope.entity;
        entity.content = CKEDITOR.instances.content.getData();
        if(!entity.title){
            alert("请输入标题");
            return;
        }
        if(!entity.categoryIdList || entity.categoryIdList.length === 0){
            alert("请选择分类");
            return;
        }
        if(!entity.content){
            alert("请输入内容");
            return;
        }
        chapterService.edit({entity:$scope.entity}).success(function (res) {
            $scope.resHandler(res, function (res) {
                if(direct !== undefined){
                    location.href = direct;
                }else {
                    location.reload();
                }
            })
        })
    };

    $scope.listInit = function () {
        chapterCategoryService.findAll().success(function (res) {
            $scope.resHandler(res, function (res) {
                temp = [];
                for(let i in res.data){
                    item = res.data[i];
                    if(item['pid'] !== 0) {
                        item['name'] = item['parent']['name'] + " -》    " + item['name'];
                    }
                    temp.push(item);
                }
                $scope.categoryList = temp;
            });
        });
        $scope.findPage();
    };

    $scope.editInit = function () {
        chapterCategoryService.findAll().success(function (res) {
            $scope.resHandler(res, function (res) {
                temp = [];
                for(let i in res.data){
                    item = res.data[i];
                    if(item['pid'] !== 0) {
                        item['name'] = item['parent']['name'] + " -》    " + item['name'];
                    }
                    temp.push(item);
                }
                $scope.categoryList = temp;
            });
        });
        chapterLabelService.findAll().success(function (res) {
            $scope.resHandler(res, function (res) {
                $scope.labelList = res.data;
            })
        });

        $(function() {
            //Initialize Select2 Elements
            $('.select2').select2();
            CKEDITOR.replace('content');

        })

        $params = $location.search();
        if($params['id'] !== undefined) {
            var queryJson = {
                entity : {
                    id : $params['id']
                }
            };
            chapterService.find(queryJson).success(function (res) {
                $scope.resHandler(res, function () {
                    $scope.entity = res.data;
                    CKEDITOR.instances.content.setData($scope.entity.content);

                    $scope.entity.categoryIdList = [];
                    for(let i in $scope.entity.categoryList){
                        var category = $scope.entity.categoryList[i];
                        $scope.entity.categoryIdList.push(category.id.toString());
                    }
                    $("#categoryIdList").val($scope.entity.categoryIdList).select2();

                    $scope.entity.labelIdList = [];
                    for(let i in $scope.entity.labelList){
                        var label = $scope.entity.labelList[i];
                        $scope.entity.labelIdList.push(label.id.toString());
                    };
                    $("#labelIdList").val($scope.entity.labelIdList).select2();

                });
            });
        }



    };

    $scope.pageHandler = function (page) {
        var records = [];
        for(let i in page.records){
            var item = page.records[i];

            item.categoryNameListString = item.categoryList.map(function (x) {
                return x.name;
            }).join(",");

            item.labelNameListString = item.labelList.map(function (x) {
                return x.name;
            }).join(",");

            records.push(item);
        }
        page.records = records;

        return page;
    }
});